using SDK.Scripts.Utils;
using SimpleJson;

namespace SDK.Scripts.Plat
{
	public class BaseGamePlat : IGamePlat
	{
		public delegate void GamePlatDelegate(string methonName, int code, string msg);

		private string _listingName = string.Empty;

		private string _appId = "0";

		private int _gameId;

		private string _domain = string.Empty;

		private string _channel = string.Empty;

		private int _gameVersion;

		private SDKPlatCfg _cfg;

		private int _userId;

		public string ListingName
		{
			get
			{
				return _listingName;
			}
			set
			{
				_listingName = value;
			}
		}

		protected SDKPlatCfg Cfg
		{
			get
			{
				return _cfg;
			}
			set
			{
				_cfg = value;
			}
		}

		public string Channel
		{
			get
			{
				return _channel;
			}
			set
			{
				_channel = value;
			}
		}

		public int GameVersion
		{
			get
			{
				return _gameVersion;
			}
			set
			{
				_gameVersion = value;
			}
		}

		public string AppId
		{
			get
			{
				return _appId;
			}
			set
			{
				_appId = value;
			}
		}

		public int GameId
		{
			get
			{
				return _gameId;
			}
			set
			{
				_gameId = value;
			}
		}

		public string Domain
		{
			get
			{
				return _domain;
			}
			set
			{
				_domain = value;
			}
		}

		public int UserID
		{
			get
			{
				return _userId;
			}
			set
			{
				_userId = value;
			}
		}

		public event GamePlatDelegate gamePlatEvent;

		public virtual void RegisterListing(string objectName)
		{
			ListingName = objectName;
		}

		public virtual void Init(SDKPlatCfg cfg)
		{
			AppId = cfg.appId;
			GameId = cfg.gameId;
			Domain = cfg.domain;
			Channel = cfg.channel;
			GameVersion = cfg.gameVersion;
			Cfg = cfg;
			JsonObject jsonObject = new JsonObject();
			jsonObject.Add("gameId", GameId.ToString());
			string str = SimpleJson.SimpleJson.SerializeObject(jsonObject);
			DoAction(platActionID.action_SetGameId, str);
			JsonObject jsonObject2 = new JsonObject();
			jsonObject2.Add("gameVersion", GameVersion.ToString());
			str = SimpleJson.SimpleJson.SerializeObject(jsonObject2);
			DoAction(platActionID.action_SetGameVersion, str);
			UpdateChannel();
		}

		private void UpdateChannel()
		{
			Channel = DoAction(platActionID.actionGetChannelId, string.Empty);
			Debuger.Log("channel = " + Channel);
		}

		public virtual void Login(bool isGuest)
		{
		}

		public virtual void SwitchAccount()
		{
		}

		public virtual void Pay(string payInfo)
		{
		}

		public virtual string DoAction(platActionID id, string str = "")
		{
			Debuger.LogDebug("action = " + id + ",msg = " + str);
			return string.Empty;
		}

		public void OnCallBack(string msg)
		{
			JsonData jsonData = Json.Deserialize<JsonData>(msg);
			string data = jsonData.data;
			int code = int.Parse(jsonData.errorCode);
			string methodName = jsonData.methodName;
			if (methodName == null)
			{
				return;
			}
			if (!(methodName == "login"))
			{
				if (!(methodName == "pay"))
				{
					if (!(methodName == "action"))
					{
						if (methodName == "switchAccount")
						{
							OnLoginBack(code, data);
						}
					}
					else
					{
						OnEventBack(code, data);
					}
				}
				else
				{
					OnPayBack(code, data);
				}
			}
			else
			{
				OnLoginBack(code, data);
			}
		}

		public void OnLoginBack(int code, string data)
		{
			if (code == 1)
			{
				ParseLoginMsg(data);
			}
			if (this.gamePlatEvent != null)
			{
				this.gamePlatEvent("login", code, data);
			}
			UpdateChannel();
		}

		private bool ParseLoginMsg(string loginMsg)
		{
			JsonObject jsonObject = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(loginMsg);
			if (jsonObject != null)
			{
				int num = -1;
				if (jsonObject.ContainsKey("ret"))
				{
					num = Util.string2int(jsonObject["ret"].ToString());
				}
				if (num < 0)
				{
					Debuger.LogDebug("login plat faild ,ret = " + num);
					return false;
				}
				if (jsonObject.ContainsKey("userId"))
				{
					UserID = Util.string2int(jsonObject["userId"].ToString());
				}
				string empty = string.Empty;
				string empty2 = string.Empty;
				if (jsonObject.ContainsKey("username"))
				{
					empty = jsonObject["username"].ToString();
					Prefs.SetItem("plat_username", empty);
				}
				if (jsonObject.ContainsKey("password"))
				{
					empty2 = jsonObject["password"].ToString();
					Prefs.SetItem("plat_password", empty2);
				}
			}
			return true;
		}

		public void OnPayBack(int code, string data)
		{
			if (this.gamePlatEvent != null)
			{
				this.gamePlatEvent("pay", code, data);
			}
		}

		public void OnEventBack(int code, string data)
		{
			if (this.gamePlatEvent != null)
			{
				this.gamePlatEvent("action", code, data);
			}
		}
	}
}
